/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servelt;

import DatabaseAccessing.Customer;
import DatabaseAccessing.CustomerTransaction;
import DatabaseAccessing.ProductModelController;
import DatabaseAccessing.Products;
import DatabaseAccessing.TransactionModelController;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author MaramZahran
 */
public class RemoveFromCart extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        int productID = Integer.parseInt(request.getParameter("ProductID"));
        ProductModelController pMC = new ProductModelController();
        TransactionModelController tMC = new TransactionModelController();
        Products p = pMC.viewProductByID(productID);
        HttpSession session = request.getSession(false);
        if (session != null) {
             Object userObj = session.getAttribute("LOGIN_USER");
                if(userObj!=null)
                {

                    Customer user = (Customer) userObj;

                 int userID = user.getCustomerId();
                 System.out.println("User ID  = "+userID);
            List oldTransList = tMC.getUndoneTransactionsForSpecificProduct(userID, productID);
            if ((oldTransList != null)&&(oldTransList.size()!=0)) {
                CustomerTransaction oldTran = ((CustomerTransaction) oldTransList.get(0));
                int amount = p.getAmount();
                amount = oldTran.getCount() + amount;
                p.setAmount(amount);
                pMC.editProduct(p);
                tMC.deleteTransaction(oldTran.getTransactionId());

            }
            List<CustomerTransaction> userCart = tMC.getUndoneTransactions(userID);
                    session.setAttribute("allproductinCarts", userCart);
                    if((userCart.size()!=0) &&(userCart!=null))
                    {
                        response.sendRedirect("CheckOut.jsp");
                    }
                    else
                    {
                        response.sendRedirect("Products.jsp");
                    }

            



        }
        else
        {
                 Object cartObj = session.getAttribute("Cart");
                if (cartObj != null) {
                    
                    List<CustomerTransaction> cart = (List) cartObj;

                    for (int i = 0; i < cart.size(); i++) {
                        CustomerTransaction obj = (CustomerTransaction) cart.get(i);
                        if (obj.getProductId() == productID) {
                            cart.remove(i);
                            session.setAttribute("Cart", cart);
                            break;
                        }
                    }
                     if((cart.size()!=0) &&(cart!=null))
                    {
                        response.sendRedirect("CheckOut.jsp");
                    }
                    else
                    {
                        response.sendRedirect("Products.jsp");
                    }
                }
        }
        }
        else
        {
            request.setAttribute("msg","cant remove the product right now");

                 response.sendRedirect("CheckOut.jsp");
               
                
        }
        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet RemoveFromCart</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet RemoveFromCart at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");
         */

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
